<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 	
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
<link rel="stylesheet" href="/css/jquery.tooltip.css" />
<script src="/js/jquery.tooltip.min.js" type="text/javascript"></script>
	<script language="javascript"><!--
	$().ready( function() {
		$("tr[id*='tdDesc_']").tooltip();
			$('#arrivalDate').datepick({minDate: '+1D', showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-1:+1'});
	    	
			$("input[name='spcReverseInventory']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcMaxCountPerOrder']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcMinCountPerOrder']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcSafeInventory']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='showSpecSeq']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcHotSaleInventory']").each(function(i,item){
				$(this).numeric();
			});
			
			$("input[id*='spcName_']").each(function(i,item){					
				if(item.value != ""){
					$(this).attr("readonly",true);
				}
			});

			$("#submitStock").click(function(){
				if ( "" == $("#arrivalDate").val() ){
					alert("请输入预计到货日");
					return false;
				}
				$("#specForm").attr("action","updateArrivalDate.do");
				$("#specForm").submit();
			});
			/*
			$("input[id*='spcHotSaleInventory_']").blur(function(){
				sumInv();
			});
			*/
			$("#submit_button").click(function(){
				try {
					$("input[id*='spcName_']").each(function(i,item){					
						if(item.value == ""){
							alert("请输入规格名称");
							item.focus();
							throw item;
						}
					});
					$("input[id*='spcReverseInventory_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入库存保留量");
							item.focus();
							throw item;
						}
						
					});
								
					$("input[id*='spcMaxCountPerOrder_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入订单,单笔最大可销售数");
							item.focus();
							throw item;
						}
						if(parseInt(item.value)<parseInt($("input[id='spcMinCountPerOrder_"+(i+1)+"']").val())){
							alert("单笔最大可销售数不可小於 单笔最小可销售数");
							item.focus();
							throw item;
						}
					});
					$("input[id*='spcMinCountPerOrder_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入订单,单笔最小可销售数");
							item.focus();
							throw item;
						}
						if(parseInt(item.value)<=parseInt(0)){
							alert("单笔最小可销售数要大於0");
							item.focus();
							throw item;
						}
					});
				
					$("input[id*='spcSafeInventory_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入安全库存量");
							item.focus();
							throw item;
						}
					});		
					$("input[id*='spcHotSaleInventory_']").each(function(i,item){
						if ( item.value == "" ){
							alert("热销 数量要大於等于0");
							item.focus();
							throw item;
						}
					});
					$("input[id*='showSeq_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入排序【0～99】");
							item.focus();
							throw item;
						}
					});
				}catch(e){
					return false;
				}

				if ($("#spcName").val() == "" && ($("#spcReverseInventory").val() != ""  || $("#spcMaxCountPerOrder").val() != "" ||  $("#spcMinCountPerOrder").val() != "")){
					alert("请输入规格名称");
					$("#spcName").focus();
					return false;
					
				}
				if ($("#spcName").val() != "" ){
					if ($("#spcReverseInventory").val() == "" ){
						alert("请输入库存保留量");
						$("#spcReverseInventory").focus();
						return false;
					}
					if ($("#spcMaxCountPerOrder").val() == "" ){
						alert("请输入订单,单笔最大可销售数");
						$("#spcMaxCountPerOrder").focus();
						return false;
					}
					if ($("#spcMinCountPerOrder").val() == "" ){
						alert("请输入订单,单笔最小可销售数");
						$("#spcMinCountPerOrder").focus();
						return false;
					}
					if(parseInt($("#spcMaxCountPerOrder").val())<parseInt($("#spcMinCountPerOrder").val())){
						alert("单笔最大可销售数不可小於 单笔最小可销售数");
						$("#spcMaxCountPerOrder").focus();
						return false;
					}
					if(parseInt($("#spcMinCountPerOrder").val())<=parseInt(0)){
						alert("单笔最小可销售数要大於0");
						item.focus();
						throw item;
					}
					if ($("#spcSafeInventory").val() == "" ){
						alert("请输入安全库存量");
						$("#spcSafeInventory").focus();
						return false;
					}
					if ($("#spcHotSaleInventory").val() == "" ){
						alert("热销 数量要大於等于0");
						$("#spcHotSaleInventory").focus();
						return false;
					}					
					if ($("#showSeq").val() == "" ){
						alert("请输入排序【0～99】");
						$("#showSeq").focus();
						return false;
					}
				}

				//若该商品已被列入搭售时,规格只能有一个,不可以再新增规格
				try{
					if(parseInt($("#specListsize").val())>0){
						var productId=$("#productId").val();
						$.getJSON("<%=ServerValue.getMidContextPath()%>/product/findAddonProdSpec.do?type=json", 
								{productId : productId}
							,function(jsonAddonProdSpec){
								if(jsonAddonProdSpec=='canOnlyOneSpec'){
									alert('该商品已被列入搭售时,规格只能有一个,不可以再新增规格');
									throw "canOnlyOneSpec";
								}
						});
					}
				}catch(e){
					return false;
				}
				$("#specForm").submit();
			});

	});
--></script>			
<h1>商品管理 - 商品规格<br/>
	<div class="f-link">
		<a href="<%=ServerValue.getMidContextPath()%>/product/list.do">回商品列表页</a>
	</div>
</h1>
<s:actionerror />
<s:actionmessage />
<s:fielderror />
<br/>
<!-- include product step menu -->
<jsp:include page="commonMenu2.jsp"/>
<div class="data-form">
	<s:form action="spec.do" method="post">
		<b>规格状态：</b>
	  <s:select name="specStatusParam" list="specStatusMenu.options" listKey="key"  listValue="value.name"/>
	  <input type="hidden" name="productId" value="${product.productId}"/>		
		<input type="button" value="查找" onclick="this.form.submit();" />
	</s:form>
</div>
<s:form action="saveSpec.do" method="post" id="specForm">
	<table class="tb-data" width="100%">
		<thead>
		 <tr>
				<th>商品名称<br/>（商品编号）</th>
				<td colspan="3">${product.productName}(${productId})</td>
		 </tr>
		 <tr>
		 		<th>热销数</th>
		 		<td><input type="text" maxlength="8" id="hotSaleInventory" size="5" value="${product.hotSaleInventory}" name="hotSaleInventory"/></td>
				<th>缺货登记数</th>
				<td>
					${stockQty}
					<fmt-rt:formatDate value="${product.arrivalDate}" pattern="yyyy-MM-dd" var="stockDate"/>
					预计到货：<input type="text" name="arrivalDate" id="arrivalDate" size="10" maxlength="10" value="${stockDate}"/>
					<input type="button" id="submitStock" value="确定"/>
				</td>
		 </tr>
		 <tr>
		 	<th><span class="txt-red">*</span>主规格名</th>
		 	<td>
		 		<s:select name="mainLabel" list="mainLabelMenu.options" listKey="key"  listValue="value.name"/>
		 		<div id="defindMainLabel" style="display:none"><input type="text" name="mainLabelDefine" maxlength="4" size="4"/></div>
		 		<br/><span class="txt-gy">主规格名上限为4个汉 字，例如颜色、材质;也可以选择自定义主规格名</span>
		 	</td>
		 	<th><span class="txt-red">*</span>次规格名</th>
		 	<td>
		 		<s:select name="secondLabel" list="secondLabelMenu.options" listKey="key"  listValue="value.name"/>
		 		<div id="defindSecondLabel" style="display:none"><input type="text" name="secondLabelDefine" maxlength="4" size="4"/></div>
		 		<br/><span class="txt-gy">次规格名上限为4个汉 字，例如尺寸、尺码;也可以选择自定义次规格名</span>
		 	</td>
		 </tr>
		 <c-rt:if test="${empty specsMap}">
		 <tr>
		 	<th>新增主/次规格</th>
		 	<td colspan="3">
		 		主规格：<input type="text" name="mainSpecName" size="50"/>
		 		次规格：<input type="text" name="secondSpecName" size="50"/>
		 		<input type="button" name="addSpecName" value="新增"/><br/>
		 		<span class="txt-gy">每个规格上限8个汉字，每个规格间用英文逗号分开；主次规格均为必填项;例如：主规格输入红色,蓝色；次规格输入S,M,L；点击新增后则显示为红色，蓝色主规格下各有次规格S,M,L。每次使用该功能新增后将会覆盖原先的规格,故要逐个新增规格可使用表格下方的[新增主规格][新增次规格]按钮。</span>
		 	</td>
		 </tr>
		 </c-rt:if>
	</table>
	<br/><br/>
	<table class="tb-data" width="100%">
		<thead>
  		<tr>
		   	<th><span class="txt-red">*</span>主规格<br/>上限16字符</th>
		   	<td colspan="12">
			   	<table class="tb-data" width="100%">
				   	<tr>
				   	<th><span class="txt-red">*</span>次规格<br/>上限16字符</th>
				   	<th>规格料号<br/>(供应商自行管理之编号)上限20字符</th>
				   	<th>规格条码<br/>(国际标准bar code)上限20字符</th>
				   	<th><span class="txt-red">*</span>单笔订单<br/>最大可销售数量</th>
				    <th><span class="txt-red">*</span>单笔订单<br/>最小可销售数量</th>
				    <th><span class="txt-red">*</span>安全库存量</th>
				    <th>可售数</th>
						<th>已售数</th>
						<th>取消数</th>
						<th><span class="txt-red">*</span>排序</th>
						<th>销售中库存查询</th>
						<th>规格状态</th>
				   	</tr>
			   	</table>
		   	</td>
	    </tr>
	  </thead>
		<tbody>
		<c-rt:forEach items="${product.specMajors}" var="major">
			<tr>
				<td>
					${major.mainSpecName}<br/>
					<input name="spcDelete" type="checkbox" value="${major.id}" ${major.isDelete eq "Y" ? "checked='checked'" : "" }/>
				</td>
				<td colspan="12">
					<table class="tb-data" width="100%">
						<c-rt:forEach items="${product.specMaps.key}" var="mainSpec" varStatus="rows_index">
							<c-rt:if test="${major.mainSpecName eq mainSpec.key">
								<c-rt:forEach items="${mainspec.value}" var="spec" varStatus="rows_index">
									<tr>
										<td>${spec.secondSpecName}</td>
										<td>${spec.specCode}</td>
										<td>${spec.barCode}</td>
										<td>${spec.reverseInventory}</td>
										<td>${spec.maxCountPerOrder}</td>
										<td>${spec.minCountPerOrder}</td>
										<td>${(specificationinventoryMap[spec.specificationId][0] - specificationinventoryMap[spec.specificationId][1] + specificationinventoryMap[spec.specificationId][2])<=0?'0':(specificationinventoryMap[spec.specificationId][0] - specificationinventoryMap[spec.specificationId][1] + specificationinventoryMap[spec.specificationId][2])}</td>
										<td>${specificationinventoryMap[spec.specificationId][1]}</td>
										<td>${specificationinventoryMap[spec.specificationId][2]}</td>
										<td><input type="text" name="showSpecSeq" maxlength="2" id="showSeq_${rows_index.count}" size="2" value="${empty spec.showSeq?'0':spec.showSeq}" /></td>
										<td><a href="<%=ServerValue.getMidContextPath()%>/product/orderNotShipping.do?specificId=${specification.specificationId}&productId=${productId}" target="_blank">查询</a></td>
										<td>修改</td>
									</tr>
								</c-rt:forEach>
								</c-rt:if>
							</c-rt:forEach>
						</table>
				</td>
			</tr>
		</c-rt:forEach>
		</tbody>
	</table>
</s:form>